package tk.redbluelotto.util;

import tk.redbluelotto.Constants;
import tk.redbluelotto.cache.RedBallCache;
import tk.redbluelotto.model.RedBall;
import tk.redbluelotto.view.RedNum;

public class CalculateUtil {

	/**
	 * 字符转int
	 * 
	 * @param stringArray
	 * @return
	 */
	public static Integer[] str2Int(String[] stringArray) {
		int length = stringArray.length;
		Integer[] integers = new Integer[length];
		for (int i = 0; i < length; i++) {
			if (!stringArray[i].equals("?"))
				integers[i] = Integer.parseInt(stringArray[i]);
		}
		return integers;
	}

	/**
	 * @param reds
	 * @return
	 */
	public static long str2Long(String redstr) {
		long[] reds = RedBallCache.getInstance().getReds();
		long tmp = 0L;
		for (String no : redstr.split(" ")) {
			tmp = tmp | reds[Integer.valueOf(no) - 1];
		}
		return tmp;
	}

	public static String long2Str(long redsLong) {
		long[] reds = RedBallCache.getInstance().getReds();
		StringBuffer result = new StringBuffer(100);
		int total = 0;
		int[] mod3 = new int[] { 0, 0, 0 };
		int[] mod2 = new int[] { 0, 0 };
		int[] prime = new int[] { 0, 0 };
		int[] mod10 = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
		for (int i = 0; i < reds.length; i++) {
			if (reds[i] == (reds[i] & redsLong)) {
				if ((i + 1) < 10)
					result.append("0");
				result.append((i + 1)).append(" ");
				total += i + 1;
				mod3[(i + 1) % 3]++;
				mod2[(i + 1) % 2]++;
				mod10[(i + 1) % 10]++;
			}
		}
		prime[0] = Long.bitCount(redsLong & Constants.getRULE().get("PRIME")[0]);
		prime[1] = 6 - prime[0];
		result.append("T[");
		if (total < 100)
			result.append("0");
		result.append(total).append("] M3[").append(mod3[0]).append(":").append(mod3[1]).append(":").append(mod3[2]).append("] M2[").append(mod2[0]).append(":").append(mod2[1])
				.append("] M10[").append(mod10[0]).append(":").append(mod10[1]).append(":").append(mod10[2]).append(":").append(mod10[3]).append(":").append(mod10[4]).append(":")
				.append(mod10[5]).append(":").append(mod10[6]).append(":").append(mod10[7]).append(":").append(mod10[8]).append(":").append(mod10[9]).append("] PC[")
				.append(prime[0]).append(":").append(prime[1]).append("]");
		return result.toString();
	}

	public static RedNum transToView(RedBall red) {
		RedNum num = new RedNum(red);
		num.mod3_0 = Long.bitCount(Constants.getRULE().get("MOD_3")[0] & red.idx);
		num.mod3_1 = Long.bitCount(Constants.getRULE().get("MOD_3")[1] & red.idx);
		num.mod3_2 = 6 - num.mod3_0 - num.mod3_1;
		num.mod2_0 = Long.bitCount(Constants.getRULE().get("MOD_2")[0] & red.idx);
		num.mod2_1 = 6 - num.mod2_0;
		num.prime = Long.bitCount(Constants.getRULE().get("PRIME")[0] & red.idx);
		num.composite = 6 - num.prime;
		num.mod10_0 = Long.bitCount(Constants.getRULE().get("MOD_10")[0] & red.idx);
		num.mod10_1 = Long.bitCount(Constants.getRULE().get("MOD_10")[1] & red.idx);
		num.mod10_2 = Long.bitCount(Constants.getRULE().get("MOD_10")[2] & red.idx);
		num.mod10_3 = Long.bitCount(Constants.getRULE().get("MOD_10")[3] & red.idx);
		num.mod10_4 = Long.bitCount(Constants.getRULE().get("MOD_10")[4] & red.idx);
		num.mod10_5 = Long.bitCount(Constants.getRULE().get("MOD_10")[5] & red.idx);
		num.mod10_6 = Long.bitCount(Constants.getRULE().get("MOD_10")[6] & red.idx);
		num.mod10_7 = Long.bitCount(Constants.getRULE().get("MOD_10")[7] & red.idx);
		num.mod10_8 = Long.bitCount(Constants.getRULE().get("MOD_10")[8] & red.idx);
		num.mod10_9 = Long.bitCount(Constants.getRULE().get("MOD_10")[9] & red.idx);
		return num;
	}

}
